package com.mdx.zhizhao.controller;


import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.fastjson.JSONObject;
import com.mdx.zhizhao.domain.Response;
import com.mdx.zhizhao.domain.Seeker;
import com.mdx.zhizhao.service.MatchService;
import com.mdx.zhizhao.service.SeekerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@CrossOrigin(origins = "http://localhost:5173")
@RequestMapping(value = "/seeker" ,produces = {MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE})
public class SeekerController {
    @Autowired
    private SeekerService seekerService;
    @Autowired
    private MatchService matchService;
     @PostMapping("/login")
    public Response<Seeker> login(@RequestBody Seeker seeker){
         System.out.println(seeker.getSeekerpassword());
        Seeker seeker1 = seekerService.login(seeker.getSeekerid(),seeker.getSeekerpassword());
        Response<Seeker> response =new Response<>();
        response.setData(seeker1);
        if(seeker1 == null){
            response.f(null,"账号或密码错误");
        }else {
            response.s(seeker1,"登陆成功");
        }
        return response;
    }
    @PostMapping("/signup")
    public Response<Seeker> signup(@RequestBody Seeker seeker){
        seekerService.signup(seeker);
        Response<Seeker> response =new Response<>();
        response.s(seeker,"成功");
        return response;
    }
    @PostMapping("/updateSeeker")
    public Response<Seeker> updateSeeker(@RequestBody Seeker seeker){
        seekerService.updateSeeker(seeker);
        Response<Seeker> response =new Response<>();
        response.s(seeker,"成功");
        return response;
    }
    @GetMapping("/{id}")
    public Response<Seeker> selectSeeker(@PathVariable String id){
        Seeker seeker = seekerService.selectSeeker(id);
        Response<Seeker> response =new Response<>();
        if(seeker == null){
            response.f(null,"无此用户");
        }else {
            response.s(seeker,"成功");
        }
        return response;
    }
    @PostMapping("/match")
    public Response<Seeker> match(@RequestBody Seeker seeker) throws NoApiKeyException, InputRequiredException, IOException {
         String b=COSandAI.select(seeker.getSeekerid());
        try {
            // 将 JSON 字符串解析为 JSONObject
            JSONObject jsonObject = JSONObject.parseObject(b);
             int status = jsonObject.getIntValue("status");
            System.out.println(status);
            if(status == 1){
                  matchService.match(seeker.getSeekerid());
            }
            // 修改 status 的值为 0
            jsonObject.put("status", 0);
            // 将修改后的 JSONObject 转换为字符串
            String modifiedJsonStr = jsonObject.toJSONString();
            System.out.println(modifiedJsonStr);
            COSandAI.update(seeker.getSeekerid(),modifiedJsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
           Response<Seeker> response = new Response<>();
            response.s(seeker, "success");
            return response;
    }

}
